package com.yonyougov.devops.cruxcisupport.commitcontext;

import com.yonyougov.devops.cruxcisupport.commitcontext.vo.CommitContextInfoVo;
import com.yonyougov.devops.cruxcisupport.commitcontext.vo.CommitContextVo;
import com.yonyougov.devops.cruxcisupport.commitdevelopuser.CommitDevelopUserDao;
import com.yonyougov.devops.cruxcisupport.commitdevelopuser.CommitDevelopUserEntity;
import com.yonyougov.devops.cruxcisupport.commitdevelopuser.CommitDevelopUserService;
import com.yonyougov.devops.cruxcisupport.commitdevelopuser.vo.ContextDevelopUserVo;
import com.yonyougov.devops.cruxcisupport.committest.dto.CommitContextDto;
import com.yonyougov.devops.cruxcisupport.util.CruxDevOpsIdGenerator;
import com.yonyougov.devops.fwc.core.dao.BaseDao;
import com.yonyougov.devops.fwc.core.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * t_commit_context服务实现
 */
@Service
public class CommitContextServiceImpl extends BaseServiceImpl<CommitContextEntity, BigInteger> implements CommitContextService {
    private final CommitContextDao commitContextDao;
    private final CommitDevelopUserService developUserService;
    private final CommitDevelopUserDao commitDevelopUserDao;

    public CommitContextServiceImpl(CommitContextDao commitContextDao, CommitDevelopUserService developUserService, CommitDevelopUserDao commitDevelopUserDao) {
        this.commitContextDao = commitContextDao;
        this.developUserService = developUserService;
        this.commitDevelopUserDao = commitDevelopUserDao;
    }

    @Override
    public BaseDao<CommitContextEntity, BigInteger> getDao() {
        return commitContextDao;
    }

    //<!--用户自定义代码开始-->

    /**
     * 根查询提测内容详情
     * 条件: 提测单id
     *
     * @param commitTestId [java.math.BigInteger]
     * @return List<CommitContextDevelopUserVo>
     */
    @Override
    public List<CommitContextVo> findListByCommitTestId(BigInteger commitTestId) {
        return commitContextDao.findListByCommitTestId(commitTestId);
    }

    /**
     * 查询提测内容详情
     * 条件: 提测单id
     *
     * @param commitTestId
     * @return [java.util.List: com.yonyougov.devops.cruxcisupport.commitcontext.vo.CommitContextInfoVo]
     */
    @Override
    public List<CommitContextInfoVo> findCommitContextByCommitTestId(BigInteger commitTestId) {
        return commitContextDao.findCommitContextByCommitTestId(commitTestId);
    }

    /**
     * 新增提测内容
     * 类型: 批量
     *
     * @param commitTestId  提测单id
     * @param commitContext 提测内容实体类
     */
    @Override
    public void AddMulCommitContext(BigInteger commitTestId, List<CommitContextDto> commitContext) {
        if (commitContext == null || commitContext.size() == 0) {
            return;
        }
        List<ContextDevelopUserVo> developUserVos = new LinkedList<>();
        List<CommitContextEntity> contextEntities = commitContext.stream().map(c -> {
            CommitContextEntity entity = new CommitContextEntity();
            BeanUtils.copyProperties(c, entity);
            entity.setId(new CruxDevOpsIdGenerator().generatorId(commitContextDao));
            entity.setCommitTestId(commitTestId);
            ContextDevelopUserVo vo = new ContextDevelopUserVo();
            vo.setCommitContextId(entity.getId());
            vo.setDevelopUserId(c.getDevelopUserId());
            developUserVos.add(vo);
            return entity;
        }).collect(Collectors.toList());
        commitContextDao.batchInsert(contextEntities);
        developUserService.batchInsert(developUserVos);
    }

    /**
     * 删除提测内容
     * 类型: 批量
     *
     * @param commitTestId 提测单id
     */
    @Override
    public void deleteByCommitTestId(BigInteger commitTestId) {
        List<CommitContextEntity> commitContextEntities = commitContextDao.findAllBy(CommitContextEntity.builder().commitTestId(commitTestId).build());
        commitContextDao.delete(CommitContextEntity.builder().commitTestId(commitTestId).build());
        for (CommitContextEntity commitContextEntity : commitContextEntities) {
            commitDevelopUserDao.delete(CommitDevelopUserEntity.builder().commitContextId(commitContextEntity.getId()).build());
        }
    }

    //<!--用户自定义代码结束-->
}
